<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>车次管理</title>
    <script type="text/javascript">
      var newNumberWin;
      var editNumberWin;
      var numberId;
      Ext.onReady(function(){
        //列车管理---------------------------------------------------------
        //生成有关列车的Grid
        var numberDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/number/getNumbers'
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'number'
          },[
            {name:'id'},
            {name:'sn'},
            {name:'line'},
            {name:'length'}
          ]
        )
        });

        var numberSM = new Ext.grid.CheckboxSelectionModel();
        var numberCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          numberSM,
          {header:'车次',sortable:true,dataIndex:'sn'},
          {header:'路线',sortable:true,dataIndex:'line'},
          {header:'一次走行公里',sortable:true,dataIndex:'length'}

        ]);

        var numberGrid =  new Ext.grid.GridPanel({
          ds: numberDS,
          cm: numberCM,
          sm: numberSM,
          width: screenWidth-240,
          height: screenHeight-310,
          iconCls: 'icon-grid',
          viewConfig:{
            forceFit:true
          },
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: numberDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newNumberWin = new Ext.Window({
                  layout: 'fit',
                  width:467,
                  height:245,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加车次',
                  autoLoad: {
                    url: "/number/newNumber",
                    scripts: true
                  }
                });
                newNumberWin.on("destroy",function(){
                  numberDS.reload();
                });
                newNumberWin.resizable = false;
                newNumberWin.show();
              }
            },{
              text: '编辑',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = numberGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  numberId = records[0].get('id');
                  editNumberWin = new Ext.Window({
                    title: '编辑车次',
                    layout:'fit',
                    width:467,
                    height:245,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/number/editNumber?id=" + numberId,
                      scripts: true
                    }
                  });
                  editNumberWin.on("destroy",function(){
                    numberDS.reload();
                  });
                  editNumberWin.resizable = false;
                  editNumberWin.show();
                }

              }
            },
            {
              text:'删除',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(numberGrid.getSelectionModel().hasSelection()){
                  var rs=numberGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var numberName='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    numberName+=data.sn+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下车次？<br><font color="red">' + numberName + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/number/delete',
                        method:'post',
                        waitMsg: '正在删除车次...',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            numberDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除车次信息。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的车次');
                }
              }
            }]
        });
        numberGrid.render('numberList');
        numberDS.load();
      })
    </script>
  </head>
  <body>
    <div id="numberList"></div>
  </body>
</html>
